{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:22.159744Z",
     "start_time": "2025-11-11T15:06:20.278853Z"
    }
   },
   "source": "%matplotlib inline",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:30.083956Z",
     "start_time": "2025-11-11T15:06:30.068716Z"
    }
   },
   "cell_type": "code",
   "source": "import math",
   "id": "45de3a58224f5082",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:34.590478Z",
     "start_time": "2025-11-11T15:06:34.573706Z"
    }
   },
   "cell_type": "code",
   "source": "import time",
   "id": "14f4e0c9382f22a0",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:39.690206Z",
     "start_time": "2025-11-11T15:06:39.674238Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "9cd630c9d761bf98",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:49.040165Z",
     "start_time": "2025-11-11T15:06:48.031547Z"
    }
   },
   "cell_type": "code",
   "source": "import torch",
   "id": "91074f48ac685031",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:51.145199Z",
     "start_time": "2025-11-11T15:06:51.129550Z"
    }
   },
   "cell_type": "code",
   "source": "print(torch.__version__)",
   "id": "ea23eb52161c7f62",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.1\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:06:54.418254Z",
     "start_time": "2025-11-11T15:06:54.386720Z"
    }
   },
   "cell_type": "code",
   "source": "print(torch.cuda.is_available())",
   "id": "a54498c790cfc19e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:09:57.996816Z",
     "start_time": "2025-11-11T15:09:57.989804Z"
    }
   },
   "cell_type": "code",
   "source": "n = 10000",
   "id": "770d9a4a62e3e9ea",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:10:12.158587Z",
     "start_time": "2025-11-11T15:10:12.154246Z"
    }
   },
   "cell_type": "code",
   "source": "a = torch.ones(n)",
   "id": "d19347285de6c7fd",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:10:20.069316Z",
     "start_time": "2025-11-11T15:10:20.065812Z"
    }
   },
   "cell_type": "code",
   "source": "b = torch.ones(n)",
   "id": "4308e6eacf50aa1f",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:22:08.490242Z",
     "start_time": "2025-11-11T15:22:08.484733Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Timer: #@save\n",
    "    def __init__(self):\n",
    "        self.times = []\n",
    "        self.start\n",
    "        \n",
    "    def start(self):\n",
    "        self.tik = time.time()\n",
    "        \n",
    "    def stop(self):\n",
    "        self.times.append(time.time() - self.tik)\n",
    "        return self.times[-1]\n",
    "    \n",
    "    def avg(self):\n",
    "        return sum(self.times) / len(self.times)\n",
    "    \n",
    "    def sum(self):\n",
    "        return sum(self.times)\n",
    "    \n",
    "    def cumsum(self):\n",
    "        return np.array(self.times).cumsum().tolist()"
   ],
   "id": "6ef89d4770db4812",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:22:09.311338Z",
     "start_time": "2025-11-11T15:22:09.296923Z"
    }
   },
   "cell_type": "code",
   "source": "## 1. ",
   "id": "cb0a8564be455dec",
   "outputs": [],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:22:10.160876Z",
     "start_time": "2025-11-11T15:22:10.088110Z"
    }
   },
   "cell_type": "code",
   "source": [
    "c = torch.zeros(n)\n",
    "timer = Timer()\n",
    "for i in range(n):\n",
    "    c[i] = a[i] + b[i]\n",
    "f'{timer.stop():.5f} sec'"
   ],
   "id": "e1539bb5bb89008f",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Timer' object has no attribute 'tik'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[23], line 5\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(n):\n\u001B[0;32m      4\u001B[0m     c[i] \u001B[38;5;241m=\u001B[39m a[i] \u001B[38;5;241m+\u001B[39m b[i]\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mtimer\u001B[38;5;241m.\u001B[39mstop()\u001B[38;5;132;01m:\u001B[39;00m\u001B[38;5;124m.5f\u001B[39m\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m sec\u001B[39m\u001B[38;5;124m'\u001B[39m\n",
      "Cell \u001B[1;32mIn[21], line 10\u001B[0m, in \u001B[0;36mTimer.stop\u001B[1;34m(self)\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mstop\u001B[39m(\u001B[38;5;28mself\u001B[39m):\n\u001B[1;32m---> 10\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtimes\u001B[38;5;241m.\u001B[39mappend(time\u001B[38;5;241m.\u001B[39mtime() \u001B[38;5;241m-\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mtik\u001B[49m)\n\u001B[0;32m     11\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtimes[\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m]\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'Timer' object has no attribute 'tik'"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T15:22:36.481251Z",
     "start_time": "2025-11-11T15:22:36.459180Z"
    }
   },
   "cell_type": "code",
   "source": "from d2l import torch as d2l",
   "id": "454edd5b3b0ef60c",
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'd2l'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mModuleNotFoundError\u001B[0m                       Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[24], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01md2l\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mimport\u001B[39;00m torch \u001B[38;5;28;01mas\u001B[39;00m d2l\n",
      "\u001B[1;31mModuleNotFoundError\u001B[0m: No module named 'd2l'"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "b6c91a0fb1b2af87"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
